package components;

import java.util.Collection;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.View;

public class Scene extends View{
	
	private int startTime;
	private int duration;
	private HashMap<Integer,Layer> layers;

	public Scene(Context context) {
		super(context);
		layers = new HashMap<Integer, Layer>();
	}
	public Scene(Context context, int startTime, int duration){
		super(context);
		this.startTime = startTime;
		this.duration = duration;
		layers = new HashMap<Integer, Layer>();
		
	}
	
	//is double buffered by design, thus no rendering problem
	public void onDraw(Canvas canvas){
		
		boolean toRepaint = false;
		for(int i = 0;i<layers.size();i++){
			Layer currentLayer = layers.get(i);
			//only repaint layers after the first invalid layer
			if(!currentLayer.isValid()){
				toRepaint = true;
			}
			if(toRepaint){
				//we let each layer draw himself, as proposed by android
				currentLayer.draw(canvas); 
			}
			currentLayer.validate();
		}
		
	}
	
	
	public void setDuration(int duration) {
		this.duration = duration;
	}

	public int getDuration() {
		return duration;
	}
	
	public void setStartTime(int start) {
		this.startTime = start;
	}

	public int getStartTime() {
		return startTime;
	}
	
	public void addLayer(int layerNumber, Layer l)
	{
		this.layers.put(layerNumber, l);
	}
	
	public HashMap<Integer,Layer> getLayerMap()
	{
		return layers;
	}
	
	public Collection<Layer> getLayers(){
		return layers.values();
	}
	
	public int getNumberOfLayers()
	{
	 return layers.size();
	}
	
	public Layer getLayer(int i)
	{
		return layers.get(i);
	}
	

}
